package com.ruoyi.common.core.page;

import cn.hutool.http.HttpStatus;
import lombok.Data;
import org.babyfish.jimmer.Page;

import java.util.List;

@Data
public class TableDataInfo<T> {

    /**
     * 总记录数
     */
    private long total;

    /**
     * 消息状态码
     */
    private int code;

    /**
     * 消息内容
     */
    private String msg;

    private List<T> rows;

    public static <T> TableDataInfo<T> build(Page<T> page) {
        return new Builder<>(page.getRows(),page.getTotalRowCount(), page.getTotalPageCount()).build();
    }

    private TableDataInfo(Builder<T> builder) {
        this.total = builder.total;
        this.code = builder.code;
        this.msg = builder.msg;
        this.rows = builder.getRows();
    }


    public static class Builder<T> extends Page<T>{

        /**
         * 总记录数
         */
        private long total;

        /**
         * 消息状态码
         */
        private int code;

        /**
         * 消息内容
         */
        private String msg;


        public Builder(List<T> rows, long totalRowCount, long totalPageCount) {
            super(rows, totalRowCount, totalPageCount);
        }

        public TableDataInfo<T> build() {
            this.msg = "查询成功";
            this.code = HttpStatus.HTTP_OK;
            this.total = this.getTotalRowCount();
            return new TableDataInfo<>(this);
        }

    }

}
